#include <stdio.h>
#include <string.h>

// i am from nanjing倒着输出
// nanjing from am i

// 逆序算法(首尾互换)
void Reserve(char *s, int len)
{
    for (int i = 0; i < len / 2; i++)
    {
        char temp = s[i];
        s[i] = s[len - 1 - i];
        s[len - 1 - i] = temp;
    }
}

void Reserve2(char *s, int len)
{
    int count = 0;
    // 一个单词结束
    for (int i = 0; i < len; i++)
    {
        if (s[i] == ' ' || s[i] == '\0')
        {
            Reserve(s + i - count, count);
            count = 0;
        }
        else
        {
            count++;
        }
    }
}
int main()
{
    char str[] = "i am from nanjing";
    Reserve(str, strlen(str));
    Reserve2(str, strlen(str));
    printf("%s\n", str);
    return 0;
}
